<?php

/*
** etc:  导出Excel格式入口类
** author: deepe.xiao
** update: 2018-10-10
*/


if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'phpexcel/PHPExcel.php';
include 'phpexcel/PHPExcel/Writer/Excel5.php'; //用于输出.xls的
class Outputexcel
{
	public $excelname;
	public $sheetname;
	public $objPHPExcel;
	public $objWriter;
	public $line;
	
	
	function Outputexcel()
	{
		$this->objPHPExcel = new PHPExcel();
		$this->objWriter = new PHPExcel_Writer_Excel5($this->objPHPExcel);
		$this->line = $this->line();
	}
	function createsheet()
	{
		$this->objPHPExcel->getActiveSheet()->setTitle($this->sheetname);
	}
	function setfield($data,$row=1)
	{
		
		$j = 0;
		foreach ($data as $v)
		{
			if(stripos($v,"@")!==false)
			{
				$result = explode("@",$v);
				$result = array("name"=>$result[0],"width"=>$result[1]);
			}else
			{
				$result = array("name"=>$v,"width"=>0);
			}
			$this->objPHPExcel->getActiveSheet()->setCellValue($this->line[$j].$row,$result["name"]);
			if($result["width"]>0)
			{
				$this->objPHPExcel->getActiveSheet()->getColumnDimension($this->line[$j])->setWidth($result["width"]);
			}
			print_r($data);
			
			$j++;
		}
		
	}
	function setdata($data,$row)
	{
		$j = 0;
		foreach ($data as $v)
		{
			$this->objPHPExcel->getActiveSheet()->setCellValue($this->line[$j].$row,$v);
			
			$j++;
		}
		
	}
	
	function output()
	{
	   ob_end_clean();
	   header("Pragma: public");
	   header("Expires: 0");
	   header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
	   header("Content-Type:application/force-download");
	   header("Content-Type:application/vnd.ms-execl");
	   header("Content-Type:application/octet-stream");
	   header("Content-Type:application/download");
	   header('Content-Disposition:attachment;filename="'.$this->excelname.'.xls"');
	   header("Content-Transfer-Encoding:binary");
	   $this->objWriter->save('php://output');
	}
	
	function line()
	{
		return array(
				  "A",
				  "B",
				  "C",
				  "D",
				  "E",
				  "F",
				  "G",
				  "H",
				  "I",
				  "J",
				  "K",
				  "L",
				  "M",
				  "N",
				  "O"
		);
	}
	
	
}

?>
